/**
 * @param {string} tiles
 * @return {number}
 */
var numTilePossibilities = function(tiles) {
  if(tiles.length == 1) return 1;
  let result = new Set(), tempArr = [], used = new Array(tiles.length).fill(false);
  tiles = tiles.split('').sort((a, b) => (a - b)).join('');

  function dfs(begin) {
    if(tempArr.length > 0) {
      result.add(tempArr.join(''));
      if(tempArr.length == tiles.length)
        return ;
    }
    for(let i = 0; i < tiles.length; i++) {
      if(i != begin && !used[i]) {
        tempArr.push(tiles[i]);
        used[i] = true;
        dfs(i);
        used[i] = false;
        tempArr.splice(tempArr.length - 1, 1);
      }
    }
  }

  dfs(-1);
  // console.log(result);
  return result.size;

};
let s = "AAB";
console.log(numTilePossibilities(s));
s = "AAABBC";
console.log(numTilePossibilities(s));
s = "V";
console.log(numTilePossibilities(s));
s = "HT";
console.log(numTilePossibilities(s));
